package com.bbt.dormassistant.service.impl;

import com.bbt.dormassistant.dao.CommentMapper;
import com.bbt.dormassistant.dao.MessageMapper;
import com.bbt.dormassistant.dao.RecordMapper;
import com.bbt.dormassistant.dao.UserInformationMapper;
import com.bbt.dormassistant.entity.Record;
import com.bbt.dormassistant.entity.UserInformation;
import com.bbt.dormassistant.service.ManageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ManageServiceImpl implements ManageService {

    @Resource
    private UserInformationMapper userInformationMapper;

    @Resource
    private RecordMapper recordMapper;

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private MessageMapper messageMapper;

    /**
     *
     * @param userId 需要被授权的用户
     * @return Boolean
     */
    @Override
    public Boolean authentication(String userId) {
        try {
            UserInformation userInformation = userInformationMapper.selectByPrimaryKey(userId);
            byte isManager = 1;
            userInformation.setIsManger(isManager);
            return userInformationMapper.updateByPrimaryKey(userInformation) == 1;
        }catch (Exception e){
            return false;
        }
    }


    /**
     *
     * @param currIndex 当前页
     * @param pageSize 页大小
     * @return List<UserInformation>
     */
    @Override
    public List<UserInformation> queryAllManagers(Integer currIndex, Integer pageSize) {
        Map<String,Object> data = new HashMap<>();
        data.put("currIndex",currIndex==null? 0:(currIndex-1)*pageSize );
        data.put("pageSize",pageSize==null? 100:pageSize);
        return userInformationMapper.selectAllManagers(data);
    }


    /**
     *
     * @param recordId 记录ID
     * @param userId 管理员ID
     * @return Boolean
     */
    @Override
    public Boolean applyRecord(Integer recordId, String userId) {
        /*
         * 查询是否为管理员、记录是否存在
         */
        Record record = recordMapper.selectByPrimaryKey(recordId);
        UserInformation userInformation = userInformationMapper.selectByPrimaryKey(userId);
        if(record == null || userInformation.getIsManger() != 1) return false;

        record.setIsApply((byte) 1);
        return recordMapper.updateByPrimaryKey(record) == 1;
    }



    /**
     *
     * @param commentId 评论ID
     * @param userId 管理员ID
     * @return Boolean
     */
    @Override
    public Boolean removeCommentById(Integer commentId, String userId) {
        /*
         * 查询是否为管理员
         */
        if(userInformationMapper.selectByPrimaryKey(userId).getIsManger() != 1) return false;

        return commentMapper.deleteByPrimaryKey(commentId) == 1;
    }

    /**
     *
     * @param messageId 需要被删除的文章ID
     * @param userId 管理员ID
     * @return Boolean
     */
    @Override
    public Boolean removeMessageById(Integer messageId, String userId) {
        try {
            return messageMapper.deleteByPrimaryKey(messageId) == 1;
        }catch (Exception e){
            return false;
        }
    }


}
